package cn.flexui.flexuiserver.service.impl;

import cn.flexui.flexuiserver.domain.entity.FileInfoDetail;
import cn.flexui.flexuiserver.mapper.FileInfoDetailMapper;
import cn.flexui.flexuiserver.service.FileInfoDetailService;
import cn.flexui.flexuiserver.utils.URLUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.get.RemoteFileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 附件信息 服务实现类
 * </p>
 *
 * @author apeto
 * @since 2024-09-17 07:08:21
 */
@Service
public class FileInfoDetailServiceImpl extends ServiceImpl<FileInfoDetailMapper, FileInfoDetail> implements FileInfoDetailService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public String getFullUrl(Long fileId) {
        FileInfoDetail fileInfoDetail = getById(fileId);
        RemoteFileInfo remoteFileInfo = fileStorageService.getFile()
                .setPlatform(fileInfoDetail.getPlatform())
                .setPath(fileInfoDetail.getPath())
                .setFilename(fileInfoDetail.getFileName())
                .getFile();
        return remoteFileInfo.getUrl();
    }

    @Override
    public RemoteFileInfo getRemoteFileInfo(Long fileId) {
        FileInfoDetail fileInfoDetail = getById(fileId);
        return fileStorageService.getFile()
                .setPlatform(fileInfoDetail.getPlatform())
                .setPath(fileInfoDetail.getPath())
                .setFilename(fileInfoDetail.getFileName())
                .getFile();
    }

    @Override
    public String getBase64(Long fileId){
        return URLUtil.urlToBase64(getFullUrl(fileId));
    }
}
